<%-- 
    Document   : registration
    Created on : Jan 13, 2012, 9:15:46 PM
    Author     : Marcelo Korn & Oleg Ruchlis
--%>

<%@page import="java.sql.SQLException"%>
<%@page import="databasePackage.DatabaseUrl"%>
<%@page import="databasePackage.ListEntry"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Registration page</title>
        <link rel="stylesheet" type="text/css" href="ex4_css.css" title="myStyle">
    </head>
    <body>
        <h1>Registration form</h1>
        <%
            // cheking if user reached here is already logged in. if so, he
            // will be redirected to the logged in page.
            String sessionUsername = (String)session.getAttribute("username");
            if(sessionUsername != null){
        %>
        <jsp:forward page="loggedin.jsp"/>
        <%
            }
            // chekinf if a form had been submited
            if ((request.getParameter("registerform")!= null) ) 
            {
                // creating a single entry with the data provided in the form.
                ListEntry entry = new ListEntry(request.getParameter("firstname"),
                                                request.getParameter("lastname"),
                                                request.getParameter("email"),
                                                request.getParameter("username"),
                                                request.getParameter("password"));
                
                // checking if the entry is valid and provides al neccesary data.
                if (entry.checkValid()) 
                {
                    // connecting to the Database.
                    DatabaseUrl db = new DatabaseUrl("ex4");
                    try
                    {
                        db.connect();
                    }
                    catch(SQLException ex)
                    {   
                        out.println("Error: " + ex.getMessage() + "\r\n" 
                                    + "Couldn't connect to the database.");
                    }
                    
                    // cheking that this entry can be added (username isn't already taken).
                    if(db.isAvailable(entry))
                    {
                        try{
                            // adding the record to the database
                            db.saveRecord(entry);
                            // setting the attribute indicating this session has a logged user.
                            session.setAttribute("username", entry.username);
                            
                            // adding the user to the list of all logged users.
                            String currentUsers = (String)application.getAttribute("users");
                            currentUsers = (currentUsers == null ? "" : currentUsers);
                            currentUsers += entry.username + "\r\n";
                            application.setAttribute("users", currentUsers);
                        // catching exception thrown by db.saveRecord
                        }catch(SQLException ex){
       %>
                           <font>User name is already taken. please choose a different one</font>
       <%
                        }
                        
        %>
        <jsp:forward page="loggedin.jsp"/>
        <%
                    }
                    // printing error because this username is already taken.
                    else
                    {
        %>
                        <font>User name is already taken. please choose a different one</font>
        <%
                    }
                }
                // printing error because one field or more is empty.
                else
                {
        %>
                    <font>Please fill all of the fields</font>
        <%
                }
            }
        %>

        <form action="registration.jsp" method="post">
            <input type="hidden" name="registerform" value="1"/>
                <table>
                    <tr>
                        <td> First name  : </td><td> <input name="firstname" size=15 type="text" /> </td> 
                    </tr>
                    <tr>
                        <td> Last name  : </td><td> <input name="lastname" size=15 type="text" /> </td> 
                    </tr>
                    <tr>
                        <td> Email Address : </td><td> <input name="email" size=30 type="text" /> </td> 
                    </tr>
                    <tr>
                        <td> Username  : </td><td> <input name="username" size=15 type="text" /> </td> 
                    </tr>
                    <tr>
                        <td> Password  : </td><td> <input name="password" size=15 type="text" /> </td> 
                    </tr>
                </table>
            <input type="submit" value="register" />
        </form>
    </BODY>
</HTML>